package cn.huihuo.weibo.controller;

import cn.huihuo.weibo.bean.participation.Participation;
import cn.huihuo.weibo.bean.participation.ParticipationBean;
import cn.huihuo.weibo.module.ParticipationModel;
import cn.huihuo.weibo.service.MessageService;
import cn.huihuo.weibo.service.ParticipationService;
import cn.jfinalbbs.common.BaseController;
import cn.jfinalbbs.common.Constants;
import cn.jfinalbbs.user.User;
import com.jfinal.plugin.activerecord.Page;

import java.util.ArrayList;

/**
 * Created by zhengjunbo on 2015/10/31.
 */
public class ParticipationController extends BaseController {

    public void index(){
        User user = getSessionAttr(Constants.USER_SESSION);

        setAttr("messageSize", user==null?"": MessageService.service.total(user.getStr("id")));

        setAttr("participationSize", user==null?"": ParticipationService.service.findTotalParticipation(user.getStr("id")));
        render("huihuo/participation.html");
    }




    public void findParticipation(){
        Integer pageStep = getParaToInt("pageStep");
        Integer count = getParaToInt("count");
        User user = getSessionAttr(Constants.USER_SESSION);
        if(user==null){
            error("请登录！");
            return;
        }
        Page<ParticipationModel> modelPage = ParticipationModel.dao.findParticipationByUserId(pageStep, count, user.getStr("id"));

        ParticipationBean participationBean = new ParticipationBean();
        participationBean.setTotalSize(String.valueOf(modelPage.getTotalPage()));
        participationBean.setParticipations(new ArrayList<>());

        for(ParticipationModel participationModel :modelPage.getList()){
            Participation participation = new Participation();
            participation.setProdUrl("/participation/zero/"+participationModel.getInt("pid")+"?productId="+participationModel.getInt("prodId"));
            participation.setProdTitle(participationModel.getStr("title"));
            participation.setPromptNumber(String.valueOf(participationModel.getInt("n_number")));
            participation.setProdPic(participationModel.getStr("inner_pic_path") == null ? participationModel.getStr("out_pic_path") : participationModel.getStr("inner_pic_path"));
            participation.setIsOpen(String.valueOf(participationModel.getInt("is_open")));
            participation.setPartPid(String.valueOf(participationModel.getInt("pid")));
            participation.setPromptNumber(usedNum(participationModel.getInt("n_number")));
            participationBean.getParticipations().add(participation);
        }

        success(participationBean);
    }

    private String usedNum(Integer num) {
        if(num <=0 ){
            return "";
        }

        if(num>9){
            return "9+";
        }
        return String.valueOf(num);
    }

    public void updateOpen(){
//        'partPid':settings.partPid,
//                'isOpen':settings.isOpen
        Integer partPid = getParaToInt("partPid");
        Boolean isOpen = getParaToBoolean("isOpen");

        ParticipationModel byId = ParticipationModel.dao.findById(partPid);
        byId.set("is_open",isOpen?1:0).update();
        success();
    }

    public void zero(){
        Integer partId = getParaToInt();
        ParticipationModel.dao.updateZero(partId);
        String productId = getPara("productId");
        //跳转到该网页
        redirect("/product/"+productId);
    }
}
